//
//  PCDApiPost.m
//  PCDBank
//
//  Created by lyy on 2021/4/19.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDApiPost.h"

@implementation PCDApiPost

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback)responseCallback {
    
    NSDictionary *dict;
    if ([data isKindOfClass:[NSDictionary class]]) {
        dict = (NSDictionary*)data;
    }else{
        dict =(NSDictionary*)[data JSONValue];
    }
    id object =[dict objectForKey:@"data"];
    NSTimeInterval timeoutInterval = 60;
    NSString *params = @"";
    if ([object isKindOfClass:NSString.class]) {
        params =(NSString *)object;
    }else if ([object isKindOfClass:NSDictionary.class]){
        params =[(NSDictionary *)object JSONRepresentation];
    }
    
    
    NSDictionary *scope = [dict objectForKey:@"scope"];
    if (![PCDUtil nilOrEmptyString:[scope objectForKey:@"timeout"]]) {
        timeoutInterval = [[scope objectForKey:@"timeout"] doubleValue]/1000;
    }
    NSString *URL =[dict objectForKey:@"url"];
//    if ([URL hasPrefix:@"undefined"]) {
//        URL = [URL stringByReplacingOccurrencesOfString:@"undefined" withString:@"/"];
//    }
    
    NSString *responseType  = [dict objectForKey:@"responseType"];
    NSString *dataType = [dict objectForKey:@"dataType"];
    
    if ([PCDUtil nilOrEmptyString:responseType]) {
        responseType = @"text";
    }
    if ([responseType isEqualToString:@"text"]) {
        if ([PCDUtil nilOrEmptyString:dataType]) {
            dataType = @"json";
        }
    }
    
    NSString *method =[dict objectForKey:@"method"];
    if ([PCDUtil nilOrEmptyString:method]) {
        method = @"POST";
    }
    
    NSString *contentType =[dict objectForKey:@"contentType"];
    if ([PCDUtil nilOrEmptyString:contentType]) {
        method = REQUEST_CONTENTTYPE_DEFAULT;
    }
    
    NSDictionary *bodyDict = [params JSONValue];
    if (!bodyDict) {
        bodyDict = @{};
    }
    
    NSString *strFailure = [dict objectForKey:@"failure"];
    NSString *strSuccess = [dict objectForKey:@"success"];
    
    //场景码
    NSString *strSceneCode = [dict objectForKey:@"SCENE_CODE"];
    //交易流水
    NSString *strSceneSeq = [dict objectForKey:@"SCENE_SEQ"];
    
    //跨步
    NSString *strBizCode = [dict objectForKey:@"BIZ_CODE"];
    NSString *strBizSeq = [dict objectForKey:@"BIZ_SEQ"];
    NSDictionary *headDic = @{@"H_BIZ_CODE":[PCDUtil safeString:strBizCode],@"H_BIZ_SEQ":[PCDUtil safeString:strBizSeq]};

    
    __block NSString *tempSuccess = strSuccess;
    __block NSString *tempFailure = strFailure;
    
    __weak typeof(self) tempSelf = self;

    NSString *strUrl =URL;
    if (![strUrl containsString:@"http"]) {
        strUrl =[PCDUtil getServerURL:URL];
    }
    
    
    [PCDNetWorkServiceGet() sendRequestWithUrlString:strUrl requestType:method contentType:contentType SceneCode:strSceneCode SceneSeq:strSceneSeq body:bodyDict head:headDic andCompletionBlock:^(PCDNetWorkItem *item, BOOL success) {
        
        NSString *responseString =item.responseString;
        
//        responseString = [responseString base64String];
        NSDictionary * responde = [responseString JSONValue];
        id  body = responde[@"body"];
        if (![body isKindOfClass:[NSDictionary class]]) {
            evaluateJavaScriptOnMainThread(tempSelf, tempSuccess, responseString);
            return;
        }
		PCDBaseDO *responseDo = [[PCDBaseDO alloc] initWithDict:responde];
		if ([@"005" isEqualToString:responseDo.Status] || [@"009" isEqualToString:responseDo.Status]) {
			[PCDUtil saveLoginSession:nil];
		}
        NSDictionary * responseDict = [responseString JSONValue];
        PCDBaseDO *baseDo = [[PCDBaseDO alloc] initWithDict:responseDict];
        
        if (success) {
            
            evaluateJavaScriptOnMainThread(tempSelf, tempSuccess, responseString);
        }else{
            if ([PCDUtil nilOrEmptyString:responseString]) {
                responseString = [NSString stringWithFormat:@"{\"statusCode\":%d}",item.error];
            }
            if ([baseDo.hStatus isEqualToString:@"COEC-1-0000015"]) {
                [PCDUtil.getCurrentVC.view showHBWait:NO isFullScreen:YES];
                evaluateJavaScriptOnMainThread(tempSelf, tempFailure,@"");
            }else{
                evaluateJavaScriptOnMainThread(tempSelf, tempFailure,responseString);
            }

        }
        tempSuccess = nil;
        tempFailure = nil;
    }];
}

@end
